﻿@using RooHui.Infrastructure.Entities.Entity;
@using RooHui.Tool;
@{
    ViewBag.Title = "GuarCompanyEdit";
    Layout = "~/Views/Shared/_Base.cshtml";
    GuaranteeCompany guacompany = ViewBag.guarcompany;
    var edittype = ViewBag.editType;
}
@section head{
<script src="@Url.Content("~/Content/roohui/jquery-validation/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Content/roohui/jquery-validation/jquery.metadata.js")"></script>
<script src="@Url.Content("~/Content/roohui/jquery-validation/messages_cn.js")"></script>
<script type="text/javascript" charset="utf-8" src="../../Content/ueditor/editor_config.js"></script>
<script type="text/javascript" charset="utf-8" src="../../Content/ueditor/editor_all_min.js"></script>
<script type="text/javascript" charset="utf-8" src="../../Content/scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript" charset="utf-8" src="../../Content/scripts/plug.js"></script>
<script type="text/javascript" charset="utf-8" src="../../Content/scripts/swfupload/jquery.swfupload.js"></script>
<script type="text/javascript" charset="utf-8" src="../../Content/scripts/swfupload/swfupload.js"></script>
}

<script type="text/javascript">
    var sumeditor = null;
    var deseditor = null;
    $(function () {
        $("#btnSave").click(function () {
            var summaryval = sumeditor.getContent().replace(/<\/?.*?>/ig, '');
            var descripvalue = deseditor.getContent().replace(/<\/?.*?>/ig, '');

            if ($('#txtCompanyName').val().trim() == "" || $('#txtCompanyName').val().length > 40) {
                $('#txtCompanyName').focus();
                $("#lblmsg").html('公司名称不能为空且不超过40个字符');
                return;
            }
            if (summaryval.trim() == "" || summaryval.length > 10000) {
                $("#lblmsg").html('公司简介不能为空且不超过10000个字符');
                return;
            }
            if (descripvalue.trim() == "" || descripvalue.length > 10000) {
                $("#lblmsg").html('公司描述不能为空且不超过10000个字符');
                return;
            }

            var imgCpyIconPath = "";
            var imgQuqImgsPath = "";
            var imgAgtImgsPath = "";
            var cpyiconlist = $("#imgCompanyIcon img");
            var quaimgList = $('#imgQualifiImages img');
            var agtimgList = $('#imgAgreementImages img');

            for (var i = 0; i < cpyiconlist.length; i++) {
                cpyiconlist[i].alt += ":";
                imgCpyIconPath += cpyiconlist[i].alt;
            }
            imgCpyIconPath = imgCpyIconPath.substr(0, imgCpyIconPath.length - 1);

            for (var i = 0; i < quaimgList.length; i++) {
                quaimgList[i].alt += ":";
                imgQuqImgsPath += quaimgList[i].alt;
            }
            imgQuqImgsPath = imgQuqImgsPath.substr(0, imgQuqImgsPath.length - 1);

            for (var i = 0; i < agtimgList.length; i++) {
                agtimgList[i].alt += ":";
                imgAgtImgsPath += agtimgList[i].alt;
            }
            imgAgtImgsPath = imgAgtImgsPath.substr(0, imgAgtImgsPath.length - 1);

            $.ajax({
                url: "/GuarCompany/GuarCompanyEdit",
                data: "guarCompanyId=" + @guacompany.ID + "&companyName=" + $("#txtCompanyName").val() + "&logoImage=" + imgCpyIconPath + "&summary=" + summaryval
                         + "&description=" + descripvalue + "&qualificationsImages=" + imgQuqImgsPath + "&agreementImages=" + imgAgtImgsPath,
                type: "post",
                success: function (result) {
                    if (result == "OK") {
                        alert("保存成功");
                        location.href = decodeURIComponent("/GuarCompany/Index");
                    }
                    else {
                        alert("保存失败。");
                    }
                }
            });
        });

        $("#btnCancel").click(function () {
            location.href = decodeURIComponent("/GuarCompany/Index");
        });

        $("#btnClose").click(function () {
            location.href = decodeURIComponent("/GuarCompany/Index");
        });
    });

    var swf = new plug.SwfUpload();
    function LoadUploadControl(imagetype) {
        new plug.PopFrame().OpenFrame('fileuploadframe', 'btnflfclose', function () {
            swf.Bind('fileupload', {
                upload_url: "/GuarCompany/SWFPrepareUpload",
                file_types: "*.jpg;*.png",
                file_types_description: "jpg或png格式",
                commitbtn: 'btnupload',
                commiturl: '/GuarCompany/SWFUpload',
                commitdata: '',
                onDialogStart: function () {
                    $('#fileuploadframe_imgbox').children('#uploadframe_nophotobox').remove();
                },
                onLoad: function () {
                    $('#fileuploadframe_imgbox').append(
                            $('<div id="uploadframe_nophotobox" style="width:130px; padding-top:120px;margin-left:auto; margin-right:auto;"/>').append(
                                $('<img src="/Content/scripts/swfupload/no_photo.png" alt="noPhoto" />')
                            )
                        );
                    $('#fileuploadframe_hint').text('请选择不大于5MB的 PNG，JPG图片。');
                },
                onAppend: function (event, file) {
                    $('#fileuploadframe_imgbox').append(
                            $('<div id="' + file.name.replace('.', '_') + '" style="margin-right:8px;margin-top:5px;float:left;width:113px;height:113px;padding:5px;border:1px solid #ccc;"></div>')
                                .append($('<div style="width:20px;margin-left:auto;margin-right:auto;padding-top:50px;"><img alt="loading" src="/Content/scripts/swfupload/frameload.gif" /></div>'))
                        );
                },
                onError: function (event, file, errorCode, message) {
                    if (errorCode == -110) {
                        alert('文件：' + file.name + '过大，请选择不超过7MB的文件');
                    }
                    else {
                        alert('文件：' + file.name + '上传失败，请稍后再试');
                        var id = file.name.replace('.', '_');
                        if ($('#' + id)) {
                            $('#' + id).remove();
                        }
                    }
                },
                onSuccess: function (event, file, serverData) {
                    var data = eval('(' + serverData + ')');
                    var id = file.name.replace('.', '_');
                    if ($('#' + id)) {
                        $('#' + id).attr('id', data.name.replace('.', '_'))
                                .html('<div><img style="width:115px;height:95px;" alt="' + data.name.replace('.', '_') + '" src="' + data.path + '" /></div>' +
                                '<div style="padding-top:2px;float:right;cursor:pointer;" onclick="javascript:swf.DeleteItem(this);"><img alt="删除" style="width:16px;height:16px;" src="/Content/scripts/swfupload/Trash_Delete.png" /></div>');
                        swf.PutImage({ name: data.name, path: data.path, horizontal: data.horizontal.toString() });
                    }
                },
                commitComplete: function (files) {
                    $('#btnflfclose').trigger('click');
                    var width = 95, height = 115;
                    for (var i = 0, len = files.length; i < len; i++) {
                        if (/^true$/i.test(files[i].horizontal.toString())) {
                            width = 115;
                            height = 95;
                        }

                        if (imagetype == 1) {
                            $('#imgCompanyIcon').html("");
                            $('#imgCompanyIcon').append(
                             $('<img alt="' + files[i].name + '" src="' + files[i].path + '" style="width:' + width + 'px;height:' + height + 'px;"/>'));
                        }
                        else if (imagetype == 2) {
                            $('#imgQualifiImages').html("");
                            $('#imgQualifiImages').append(
                             $('<img alt="' + files[i].name + '" src="' + files[i].path + '" style="width:' + width + 'px;height:' + height + 'px;"/>'));
                        }
                        else if (imagetype == 3) {
                            $('#imgAgreementImages').html("");
                            $('#imgAgreementImages').append(
                             $('<img alt="' + files[i].name + '" src="' + files[i].path + '" style="width:' + width + 'px;height:' + height + 'px;"/>'));
                        }
                    }
                }
            });
        }, function () {
            $('#fileuploadframe_imgbox').html('');
            $('#fileupload').html('');
            swf.uninstall();
        });
    }

</script>

<div style="overflow-x:hidder;overflow-y:auto;height:100%">
<form action="/" method="post" id="frmGuaranteeCompany" name="frmGuaranteeCompany" style="background-color: white">
<div id="form1" class="liger-form" style="padding-top: 10px;">
    <table cellpadding="0" cellspacing="0" class="l-table-edit">
        <tr>
            <td align="right" class="l-table-edit-td" style="width:80px;">
                <span class="l-star">*</span>&nbsp;公司名称:
            </td>
            <td align="left" class="l-table-edit-td">
            @{
                if (edittype == 1){
                    <input name="txtCompanyName" type="text" id="txtCompanyName" ltype="text" value="@guacompany.CompanyName" ligerui="width:250" style="width:250;" validate="{required:true,notnull:true,maxlength:40}"  nullText="不能为空且不超过40个字"/>
                }
                else if (edittype == -1)
                {
                    <span id="productName">@guacompany.CompanyName</span>
                }}
            </td>
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td" style="width:80px;">
                &nbsp;公司图标: 
            </td>
            <td align="left" class="l-table-edit-td">
            @{
                if (edittype == 1)
                {
                    <input type="button" id="btnCpyIconUpload" value="点击上传" class="l-button" onclick="LoadUploadControl(1)" />
                }
             }
                <div id="imgCompanyIcon" style="width:100%; height:100%;">
                @{
                var fixstr1 = "/UploadFile/GuaCpyResource/data/";
                var lgimglist = guacompany.LogoImage.Split(':');
                foreach (var lgimg in lgimglist)
                {
                    var tempstr1 = fixstr1 + lgimg;
                    <img alt="@lgimg" src="@tempstr1" style="width:115px; height:95px;"/>
                }}
                </div>
            </td>
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td" style="width:80px;">
                <span class="l-star">*</span>&nbsp;公司简介:
            </td>
            <td align="left" class="l-table-edit-td">
             @{
                 if (edittype == 1)
                 {
                    <script name="txtSummary" id="txtSummary" type="text/plain">@guacompany.Summary</script>
                    <script type="text/javascript">
                        window.UEDITOR_CONFIG.initialFrameWidth = 500;
                        window.UEDITOR_CONFIG.initialFrameHeight = 100;
                        sumeditor = UE.getEditor('txtSummary');
                    </script>
                 }
                 else if (edittype == -1)
                 {
                    <p id="summary" style="width:800px; word-break:break-all; word-wrap:break-word;">@guacompany.Summary</p>
                 }}
            </td>
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td" style="width:80px;">
                &nbsp;公司描述:
            </td>
            <td align="left" class="l-table-edit-td">
            @{
                if (edittype == 1)
                {
                    <script name="txtDescription" id="txtDescription" type="text/plain">@guacompany.Description</script>
                    <script type="text/javascript">
                        window.UEDITOR_CONFIG.initialFrameWidth = 500;
                        window.UEDITOR_CONFIG.initialFrameHeight = 150;
                        deseditor = UE.getEditor('txtDescription');
                    </script>
                }
                else if (edittype == -1)
                {
                    <p id="description" style="width:800px; word-break:break-all; word-wrap:break-word;">@guacompany.Description</p>
                }}
            </td>
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td" style="width:80px;">
                <span class="l-star">*</span>&nbsp;资质图片:
            </td>
            <td align="left" class="l-table-edit-td">
            @{
                if (edittype == 1)
                {
                    <input type="button" id="btnQualifiImagesUpload" value="点击上传" class="l-button" onclick="LoadUploadControl(2)" />
                }
             }
                <div id="imgQualifiImages" style="width:100%; height:100%;">
                @{
                var fixstr2 = "/UploadFile/GuaCpyResource/data/";
                var quaimglist = guacompany.QualificationsImages.Split(':');
                foreach (var quaimg in quaimglist)
                {
                    var tempstr2 = fixstr2 + quaimg;
                    <img alt="@quaimg" src="@tempstr2" style="width:115px; height:95px;"/>
                }}
                </div>
            </td>
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td" style="width:80px;">
                <span class="l-star">*</span>&nbsp;协议图片:
            </td>
            <td align="left" class="l-table-edit-td">
            @{
                if (edittype == 1)
                {
                    <input type="button" id="btnAgreementImagesUpload" value="点击上传" class="l-button" onclick="LoadUploadControl(3)" />
                }
             }
                <div id="imgAgreementImages" style="width:100%; height:100%;">
                @{
                var fixstr3 = "/UploadFile/GuaCpyResource/data/";
                var agtimglist = guacompany.AgreementImages.Split(':');
                foreach (var agtimg in agtimglist)
                {
                    var tempstr3 = fixstr3 + agtimg;
                    <img alt="@agtimg" src="@tempstr3" style="width:115px; height:95px;"/>
                }}
                </div>
            </td>
        </tr>
        <tr>
            <td align="left" class="l-table-edit-td">  
            </td>
            <td align="left">
            </td>
        </tr>
        <tr>
            <td align="left" class="l-table-edit-td"> 
            </td>
            <td align="left">
            @{
                if (edittype == 1)
                {
                    <input type="button" value="保存" id="btnSave" name="btnSave" class="l-button" />
                    <input type="button" value="取消" id="btnCancel" name="btnCancel" class="l-button" />
                }
                else if (edittype == -1)
                {
                    <input type="button" value="关闭" id="btnClose" name="btnClose" class="l-button" />
                }}
            </td>
        </tr>
    </table>
    <br />
</div>
<div id="fileuploadframe" style="display:none; position:absolute; width:550px;height:481px;z-index:99999;">
<div style="height:25px;width:100%; border-bottom:1px solid #ccc">
<div style="float:right;width:80px; height:23px; line-height:23px;">
    <a href="javascript:;"><img alt="关闭" style="border:none;" id="btnflfclose" src="../../Content/scripts/swfupload/closelabel.gif"/></a>
</div>
</div>
<div style="width:100%;height:455px;">
    <div id="fileuploadframe_imgbox" style="width:100%;height:410px; overflow:auto;">
    </div>
    <div id="fileuploadframe_controlbox" style="width:100%;height:45px;">
        <div id="fileupload" style="float:left;width:130px;"></div>
        <div id="fileuploadframe_hint" style="font-size:12px;padding-top:20px; float:left;width:320px;"></div>
        <div id="fileuploadframe_btnsubmit" style="float:right;"><input id="btnupload" type="button" class="l-button" value="完成" /></div>
    </div>
</div>

</div>
</form>
</div>
<script type="text/javascript">
    $(function () {
        $.validator.addMethod(
                    "notnull",
                    function (value, element, regexp) {
                        if (!value) return true;
                        return !$(element).hasClass("l-text-field-null");
                    },
                    "不能为空"
            );

        $.metadata.setType("attr", "validate");
        var v = $("#frmGuaranteeCompany").validate({
            //debug: true,
            errorPlacement: function (lable, element) {
                if (element.hasClass("l-textarea")) {
                    element.ligerTip({ content: lable.html(), target: element[0] });
                }
                else if (element.hasClass("l-text-field")) {
                    element.parent().ligerTip({ content: lable.html(), target: element[0] });
                }
                else {
                    lable.appendTo(element.parents("td:first").next("td"));
                }
            },
            success: function (lable) {
                lable.ligerHideTip();
                lable.remove();
            },
            submitHandler: function () {
                //todo
            }
        });
        $("#frmGuaranteeCompany").ligerForm();
    });

</script>